from pynumpak.general import *
from pynumpak.integrate import inner_product
from pynumpak.linalg import poly_fit, as_vec
from pynumpak.type import *


def continous_poly_fit(func: func_t, ab: Interval, n: int):
    return Poly_fit(func=func, ab=ab, n=n)


def discrete_poly_fit(a, b, n: int):
    return Polynomial(poly_fit(a=as_vec(a), b=as_vec(b), n=n))


class Poly_fit(Func):
    def __init__(self, func: func_t, ab: Interval, n: int):
        coe = []
        for i in range(n + 1):
            tp = Legendre(i, ab)
            coe.append(inner_product(tp, func, ab) / tp.squared_norm)
        self.leg = Legendre(coe, ab)
        pass

    def compute(self, x: float):
        return self.leg(x)
